package com.example.lenovo.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class ceshi5 extends Fragment {

    //定义的集合对象
    private ListView listView1;
    //定义的固定列表内容
    private String[] title=new String[]{"空气温度值","土壤湿度值","CO2浓度值"};
    //数据适配器
    private MyAdapter myAdapter1;
    //创建并且执行视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup, Bundle bundle){

        View view=inflater.inflate( R.layout.ceshi_main,viewgroup,false);

        listView1=(ListView)view.findViewById(R.id.ceshi_main_listview1);

        myAdapter1=new MyAdapter(title);
        //给列表添加适配器
        listView1.setAdapter( myAdapter1 );
        //给列表中的每一行添加点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dialog(position);
            }
        });
        return view;
    }

    //数据适配器，重点将数据于控件进行绑定
    public class MyAdapter extends BaseAdapter {
        //接受外界传入的数据
        private String[] titles;
        //通过构造函数将外界的值和内部变量进行交互
        public MyAdapter(String[] title){
            this.titles=title;

        }
        @Override
        public int getCount() {

            return this.titles.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //声明一个视图对象，获取列表中的子项内容
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.ceshi_main_item,null);
            TextView textView=view.findViewById(R.id.ceshi_main_item_title1);
            textView.setText( titles[position] );
            return view;
        }
    }

    //通过模板窗体
    public void dialog(int position) {
        //声明一个对话框模板窗体
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        //设置弹出窗体的标题
        builder.setTitle("子页面"+title[position]);
        //弹出窗体内的信息
        String str1="测试1";
        String str2="测试2";
        int int1=1,int2=2;
        //设置窗体标签的详细说明
        switch(position) {
            case 0:
                int1=12;
                int2=24;
                str1 += "(单位)";
                str2 += "(单位)";
                break;
            case 1:
                int1=12;
                int2=24;
                str1 += "(单位)";
                str2 += "(单位)";
                break;
        }
        //获取并设置模板对象
        View view=LayoutInflater.from( getActivity() ).inflate(R.layout.dialog,null);
        //根据对象获取模板当中的控件
        //final哪来的
        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        final EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview_title2);
        //给控件赋值
        textView1.setText(str1);
        textView2.setText(str2);
        //由数字转化为字符串可以在变量面前添加“”空值
        editText1.setText("" + int1);
        editText2.setText("" + int2);
        //将设置好的控件加载到窗体中
        builder.setView( view );

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr=editText1.getText().toString();
            }
        });
        //当选择弹出窗体的取消按钮时触发的事件
        builder.setNegativeButton("取消",null);
        //创建窗体并经行显示
        builder.create().show();
    }
}
